package cn._51doit.flink.day02.sources;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * Flink从Kafka中读取数据
 * 1.KafkaSource是多并行的Source吗？
 * 2.KafkaSource对应的subtask数量如果与topic的分区数量不一致会怎样
 * 3.同一个组消费的数据如果重启，会不会重新消费（消费者有没有记录偏移量，偏移量记录到哪了）
 */
public class KafkaSourceDemo1 {

    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);

        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "node-1.51doit.cn:9092,node-2.51doit.cn:9092,node-3.51doit.cn:9092");
        properties.setProperty("group.id", "test");
        //没有偏移量从头开始读，有偏移量接着偏移量读
        properties.setProperty("auto.offset.reset", "earliest");
        //不将偏移量写入到Kafka特殊的topic中
        properties.setProperty("enable.auto.commit", "false");
        //调用addSource添加Flink的Connector
        FlinkKafkaConsumer<String> flinkKafkaConsumer = new FlinkKafkaConsumer<String>(
                "test",
                new SimpleStringSchema(),
                properties
        );

        DataStreamSource<String> lines = env.addSource(flinkKafkaConsumer);

        lines.print().setParallelism(3);

        env.execute();


    }

}
